﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace YJ.Data.MSSQL
{
    public class WorkCalendar : YJ.Data.Interface.IWorkCalendar
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public WorkCalendar()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.WorkCalendar实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.WorkCalendar model)
        {
            string sql = @"INSERT INTO WorkCalendar
				(WorkDate) 
				VALUES(@WorkDate)";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@WorkDate", SqlDbType.Date, -1){ Value = model.WorkDate }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.WorkCalendar实体类</param>
        public int Update(YJ.Data.Model.WorkCalendar model)
        {
            string sql = @"UPDATE WorkCalendar SET 
				
				WHERE WorkDate=@WorkDate";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@WorkDate", SqlDbType.Date, -1){ Value = model.WorkDate }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(DateTime workdate)
        {
            string sql = "DELETE FROM WorkCalendar WHERE WorkDate=@WorkDate";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@WorkDate", SqlDbType.Date){ Value = workdate }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.WorkCalendar> DataReaderToList(SqlDataReader dataReader)
        {
            List<YJ.Data.Model.WorkCalendar> List = new List<YJ.Data.Model.WorkCalendar>();
            YJ.Data.Model.WorkCalendar model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.WorkCalendar();
                model.WorkDate = dataReader.GetDateTime(0);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.WorkCalendar> GetAll()
        {
            string sql = "SELECT * FROM WorkCalendar";
            SqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.WorkCalendar> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM WorkCalendar";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.WorkCalendar Get(DateTime workdate)
        {
            string sql = "SELECT * FROM WorkCalendar WHERE WorkDate=@WorkDate";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@WorkDate", SqlDbType.Date){ Value = workdate }
			};
            SqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.WorkCalendar> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }


        /// <summary>
        /// 删除一年的记录
        /// </summary>
        public int Delete(int year)
        {
            string sql = "DELETE FROM WorkCalendar WHERE YEAR(WorkDate)=@WorkDate";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@WorkDate", SqlDbType.Int){ Value = year }
			};
            return dbHelper.Execute(sql, parameters);
        }

        /// <summary>
        /// 查询一年所有记录
        /// </summary>
        public List<YJ.Data.Model.WorkCalendar> GetAll(int year)
        {
            string sql = "SELECT * FROM WorkCalendar WHERE YEAR(WorkDate)=@WorkDate";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@WorkDate", SqlDbType.Int){ Value = year }
			};
            SqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.WorkCalendar> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
    }
}